﻿using SmartHotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartHotel.DAL
{
    public class JifenDAL
    {
        /// <summary>
        /// 通过构造函数的形式注入上下文
        /// </summary>
        /// <param name="hotelContext"></param>
        public JifenDAL(HotelContext hotelContext)
        {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="jiFen"></param>
        /// <returns></returns>
        public int Add(JiFen jiFen) {

            HotelContext.JiFens.Add(jiFen);
            return HotelContext.SaveChanges();
        }

        /// <summary>
        /// 根据积分id查找积分
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JiFen GetJifenById(int id) {
            return HotelContext.JiFens.Where(x => x.Id == id).FirstOrDefault();
        }

        //public int Update(JiFen jiFen) {
        //    var jifen1 = GetJifenById(jiFen.Id);
        //    if (jifen1 != null) {
        //        jifen1.Reason = jiFen.Reason;
        //    }
        //    return HotelContext.SaveChanges();
        //}

        /// <summary>
        /// 积分列表分页
        /// </summary>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页条数</param>
        /// <param name="count">后端查询后返回的总条数</param>
        /// <returns></returns>
        public List<JiFen> GetJifenPages(int index,int size,out int count) {
            var jifens = HotelContext.JiFens.AsQueryable(); //select * from jifen where 1=1
            count = jifens.Count();// 执行了上面的sql: selec count(*) from (select * from jifen where 1=1) ;
            var list  = jifens.OrderBy(x=>x.Id)//排序
                          .Skip((index-1)*size)//跳过
                          .Take(size)//取
                          .ToList();//
            return list;
        }

    }
}
